package com.xhj.bigfactory.bigFactory.volatile0;

import static java.lang.System.out;
import static java.lang.Thread.currentThread;

/**
 * -
 *
 * @author 周沫 @Date 2021-07-25 @Time 1:33
 */
public class SingletonDemo {
  public static void main(String[] args) {
    for (int i = 0; i < 12; i++) {
      new Thread(
              () -> {
                out.println(currentThread().getName() + " come in...");
                Singleton singleton = Singleton.getData3();
              },
              "thread" + i)
          .start();
    }
  }
}

class Singleton {
  private static Singleton instance = null;

  private Singleton() {
    out.println("constructor。。。。。。");
  }

  public static Singleton getInstance() {
    return instance == null ? instance = new Singleton() : instance;
  }

  public static synchronized Singleton getData2() {
    return instance == null ? instance = new Singleton() : instance;
  }

  public static Singleton getData3() {
    if (instance == null) {
      synchronized (Singleton.class) {
        return instance == null ? instance = new Singleton() : instance;
      }
    }
    return instance;
  }
}
